<template>
  <el-dialog
    :visible.sync="dialogVisible"
    :title="title"
    :close-on-click-modal="false"
    width="700px"
    @close="close"
  >
    <el-form ref="form" v-loading="loading" :model="form" :rules="rules" label-width="120px">
      <el-form-item label="所属门店：" prop="shopId">
        <el-select v-model="form.shopId" placeholder="请选择">
          <el-option
            v-for="item in $store.state.user.manageShopListExcludeHeadquarters"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="云喇叭名称：" prop="name">
        <el-input v-model="form.name" placeholder="请输入名称" />
      </el-form-item>

      <el-form-item label="品牌：" prop="brandType">
        <el-radio-group v-model="form.brandType">
          <el-radio
            v-for="(val,key,index) in brandOptions"
            :key="index"
            :label="Number(key)"
          >{{ val }}</el-radio>
        </el-radio-group>
      </el-form-item>

      <el-form-item v-if="form.brandType===1000" label="接口版本：" prop="model">
        <el-radio v-model="form.model" label="V1">V1版本</el-radio>
        <el-radio v-model="form.model" label="V2">V2版本</el-radio>
      </el-form-item>

      <el-form-item label="第三方SN号：" prop="supplierDeviceSn">
        <el-input v-model="form.supplierDeviceSn" placeholder="请输入SN号" />
      </el-form-item>

      <el-form-item label="备注：" prop="tag">
        <el-input v-model="form.remark" maxlength="30" placeholder="说明备注" />
      </el-form-item>
    </el-form>

    <div slot="footer" class="dialog-footer">
      <el-button size="small" round @click="hide">取 消</el-button>
      <el-button size="small" round type="primary" @click="handleSubmit('form')">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { validateStrLen } from '@/utils/validate.js'
import { addSpeaker, updateSpeaker } from '@/api/speaker'
export default {
  props: {
    shopId: {
      require: true,
      type: Number
    }
  },
  data() {
    // 名称
    const validateName = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入云喇叭名称'))
      } else if (!validateStrLen(value, 30)) {
        callback(new Error('长度不超过15个汉字或30个字符'))
      } else {
        callback()
      }
    }
    return {
      rules: {
        shopId: [
          {
            required: true,
            message: '请选择组织架构',
            trigger: ['change']
          }
        ],
        name: [
          {
            required: true,
            validator: validateName,
            trigger: ['blur', 'change']
          }
        ],
        brandType: [
          {
            required: true,
            message: '请选择品牌',
            trigger: ['change']
          }
        ],
        supplierDeviceSn: [
          {
            required: true,
            message: '请输入第三方SN号'
          }
        ],
        remark: [
          {
            message: '请输入',
            trigger: 'blur'
          }
        ]
      },
      // 配置
      brandOptions: {
        1000: '智联博众',
        2000: '博实结'
      },
      title: null,
      resolve: null,
      loading: false,
      reject: null,
      mode: null,
      dialogVisible: null,
      form: {
        status: 1,
        shopId: '',
        name: '',
        brandType: 1000,
        model: '',
        supplierDeviceSn: '',
        remark: ''
      }
    }
  },
  methods: {
    openAdd() {
      this.mode = 'add'
      this.title = '新增云喇叭'
      this.form.shopId = this.shopId
      this.show()
      return new Promise((resolve, reject) => {
        this.resolve = resolve
        this.reject = reject
      })
    },

    openEdit(row) {
      this.title = '编辑云喇叭'
      this.mode = 'edit'
      this.form = Object.assign({}, row)
      this.show()
      return new Promise((resolve, reject) => {
        this.resolve = resolve
        this.reject = reject
      })
    },

    close() {
      this.form = {
        status: 1,
        shopId: '',
        name: '',
        brandType: 1000,
        model: '',
        supplierDeviceSn: '',
        remark: ''
      }
      this.loading = false
      this.$refs.form.resetFields()
    },

    show() {
      this.dialogVisible = true
    },

    hide() {
      this.resolve = null
      this.reject = null
      this.dialogVisible = false
    },

    handleSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.loading = true
          let apiFun = updateSpeaker
          if (this.mode === 'add') {
            apiFun = addSpeaker
          }
          apiFun(this.form)
            .then((res) => {
              this.$notify({
                title: '成功',
                message: this.mode === 'add' ? '新增成功' : '更新成功',
                type: 'success',
                duration: 2000,
                offset: 40
              })
              this.resolve && this.resolve()
              this.hide()
            })
            .catch(() => {})
            .finally(() => {
              this.loading = false
            })
        }
      })
    }
  }
}
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
  .el-select {
    width: 100%;
  }
  .el-checkbox {
    width: 120px;
  }
</style>
